//
//  DYZMySharedButtonController.m
//  DYZCars
//
//  Created by MF on 16/8/20.
//  Copyright © 2016年 MF. All rights reserved.
//

#import "DYZMySharedButtonController.h"
#import "Masonry.h"
#import "NSAttributedString+Additon.h"


@interface DYZMySharedButtonController ()

@property (nonatomic,weak) UIView *shareView;
@end

@implementation DYZMySharedButtonController

- (void)viewDidLoad {
    [super viewDidLoad];
    UIView *shareView = [[UIView alloc]init];
    shareView.backgroundColor = [UIColor orangeColor];
    self.shareView = shareView;
    [self.view addSubview:shareView];
    [shareView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.left.right.equalTo(self.view);
        make.height.offset(150);
    }];
    [self setupUI];
    // Do any additional setup after loading the view.
}
//-(void)viewWillAppear:(BOOL)animated{

//}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (void)setupUI
{
    //
    //    self.backgroundColor = [UIColor colorWithHex:0x3a3a3a];
    
    // 创建四个button
    UIButton* btn1 = [self loadHomeTopViewButtonWithTitle:@"扫一扫" andImageName:@"u832"];
    UIButton* btn2 = [self loadHomeTopViewButtonWithTitle:@"付款" andImageName:@"u832"];
    UIButton* btn3 = [self loadHomeTopViewButtonWithTitle:@"卡卷" andImageName:@"u832"];
    UIButton* btn4 = [self loadHomeTopViewButtonWithTitle:@"咻一咻" andImageName:@"u832"];
    
    // 添加四个button
    [self.shareView addSubview:btn1];
    [self.shareView addSubview:btn2];
    [self.shareView addSubview:btn3];
    [self.shareView addSubview:btn4];
    NSArray *subArr = @[btn1,btn2,btn3,btn4];
    // 监听按钮
    for (int i = 0; i < subArr.count; i++) {
        UIButton* btn = subArr[i];
        [btn addTarget:self action:@selector(topViewButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    
    // 自动布局四个按钮
    [subArr mas_makeConstraints:^(MASConstraintMaker* make) {
        make.top.bottom.offset(0);
    }];
    
    for (int i = 0; i < subArr.count - 1; i++) {
        UIView* v = subArr[i];
        UIView* nextV = subArr[i + 1];
        
        if (i == 0) { // 第一个按钮的左边
            [v mas_makeConstraints:^(MASConstraintMaker* make) {
                make.left.offset(0);
            }];
        }
        
        if (i == subArr.count - 2) { // 最后一个按钮的右边
            [nextV mas_makeConstraints:^(MASConstraintMaker* make) {
                make.right.offset(0);
            }];
        }
        
        // 四个按钮相互等宽
        [nextV mas_makeConstraints:^(MASConstraintMaker* make) {
            make.left.equalTo(v.mas_right);
            make.width.equalTo(v);
            
        }];
    }
}

// 按钮的点击事件
- (void)topViewButtonClick:(UIButton*)sender
{
    NSLog(@"567");
    // 4.判断代理方法是否能够响应
    //    if ([self.delegate respondsToSelector:@selector(homeTopView:didClickHomeTopViewButtonWithType:)]) {
    //        // 5.执行代理方法
    //        [self.delegate homeTopView:self didClickHomeTopViewButtonWithType:sender.tag];
    //    }
}

/**
 *  创建'扫一扫'的按钮
 *
 *  @param title     标题
 *  @param imageName 图片名称
 *
 *  @return 已经创建好的按钮
 */
- (UIButton*)loadHomeTopViewButtonWithTitle:(NSString*)title andImageName:(NSString*)imageName
{
    // 创建属性文本
    NSAttributedString* str = [NSAttributedString imageTextWithImage:[UIImage imageNamed:imageName] imageWH:35 title:title fontSize:14 titleColor:[UIColor whiteColor] spacing:7];
    
    // 创建扫一扫的button
    UIButton* topViewButton = [[UIButton alloc] init];
    
    [topViewButton setAttributedTitle:str forState:UIControlStateNormal];
    
    // 设置换行
    topViewButton.titleLabel.numberOfLines = 0;
    // 文字居中
    topViewButton.titleLabel.textAlignment = NSTextAlignmentCenter;
    
    // 调整大小
    [topViewButton sizeToFit];
    
    // 根据传过来的枚举设置tag (用来区分不同的按钮)
    //    topViewButton.tag = type;
    
    return topViewButton;
}
@end

